package org.lilred.nzbberry.views;


import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Graphics;

import org.lilred.nzbberry.config.Resizer;
import org.lilred.nzbberry.models.HistoryNzbModel;
import org.lilred.nzbberry.util.FontFactory;
import org.lilred.nzbberry.util.ScaleImage;

public class HistorySlotView extends SlotView {
	HistoryNzbModel model;
	static Bitmap successBitmap;
	static Bitmap  failureBitmap;
	static Bitmap warningBitmap;
	
	public HistorySlotView(HistoryNzbModel model) {
		this.model = model;
		initializeIcons();
	}

	private void initializeIcons() {
		EncodedImage ei = null;
		if (successBitmap == null) {
			ei = EncodedImage.getEncodedImageResource("HistorySuccess.png");
			ei = ScaleImage.toWidth(ei, Resizer.resize(ei.getWidth()));
			successBitmap = ei.getBitmap();
		}
		if (failureBitmap == null) {
			ei = EncodedImage.getEncodedImageResource("HistoryFail.png");
			ei = ScaleImage.toWidth(ei, Resizer.resize(ei.getWidth()));
			failureBitmap = ei.getBitmap();
		}
		if (warningBitmap == null) {
			ei = EncodedImage.getEncodedImageResource("HistoryWarning.png");
			ei = ScaleImage.toWidth(ei, Resizer.resize(ei.getWidth()));
			warningBitmap = ei.getBitmap();
			
		}
	}

	public void setModel(HistoryNzbModel m) {
		this.model = m;
		invalidate();
	}

	protected void paint(Graphics g) {

		if (isFocus()) {
			g.setColor(0xf9f9f9);
		} else if (model.getIndex() % 2 == 1) {
			g.setColor(0xe0e0e0); 
		} else {
			g.setColor(0xd0d0d0);
		}
		g.setGlobalAlpha(220);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setGlobalAlpha(255);

		//Draw the white line on the top
		g.setColor(0xFFFFFF);
		g.drawLine(0, 0, getWidth(), 0);

		//Draw the dark grey line on the bottom
		g.setColor(0xb1b1b1);
		g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);

		//Draw the focus border
		g.setColor(0xFFFF00);
		if (isFocus()) {
			g.drawRect(0, 0, getWidth(), getHeight());
		}
		g.setColor(0x000000);

		g.setColor(0x000000);
		g.drawText(model.getFilename(), successBitmap.getWidth() + 2 * Resizer.resize(6), Resizer.resize(9));
		Bitmap bitmapToDraw = null;
		String secondLineText = new String();
		if (model.getStatus().compareTo("Completed") == 0) {
			g.setColor(0x00af00);
			secondLineText = "Completed " + model.getCompletionDate().toString();
			bitmapToDraw = successBitmap;
		}
		else if (model.getStatus().compareTo("Failed") == 0) {
			g.setColor(0xFF0000);
			secondLineText = model.getErrorMessage();
			if (secondLineText.length() == 0) secondLineText = "Unknown Error";
			bitmapToDraw = failureBitmap;
		} else {
			secondLineText = model.getActionLine();
			bitmapToDraw = warningBitmap;
		}
		g.setFont(FontFactory.getFont(Resizer.resize(20)));
		g.setFont(FontFactory.getFont(Resizer.resize(16)));
		g.drawText(secondLineText, bitmapToDraw.getWidth() + 2 * Resizer.resize(6), Resizer.resize(36));
		g.drawBitmap(Resizer.resize(6), getHeight() / 2 - bitmapToDraw.getHeight() / 2, bitmapToDraw.getWidth(), bitmapToDraw.getHeight(), bitmapToDraw, 0, 0);
	}

	protected void onFocus(int direction) {
		super.onFocus(direction);
		AppView.getInstance().appManager.updateFooter("Position: " + (model.getIndex() + 1));
	}

}
